'use strict';

define([
	'jquery',
	'underscore',
	'backbone',
	'bootstrap',
	'text!modules/supplier/templates/supplierModelDialogTmpl.html',
	'supplierModel'
],function(
	$,
	_,
	Backbone,
	Bootstrap,
	SupplierModelDialogTmpl,
	SupplierM
){
	var supplierModelDialogView=Backbone.View.extend({
		
		viewName:'supplierModelDialogView',
		
		el:'#modelSupplier',
		
		template:_.template(SupplierModelDialogTmpl),
		
		modelDialog:"#modelSupplierHandle",
		
		supplierModel:new SupplierM(),
		
		subControls:{
			 Code:function(){return $('#Code');},
			 Name:function(){return $('#Name');},
			 LogoUrl:function(){return $('#LogoUrl');},
			 Location:function(){return $('#Location');},
			 Contact:function(){return $('#Contact');},
			 Tel:function(){return $('#Tel');},
			 Description:function(){return $('#Description');},
			 btnAddSupplier:function(){return $('#btnAddSupplier');},
			 btnUpdateSupplier:function(){return $('#btnUpdateSupplier');},
			 btnDeleteSupplier:function(){return $('#btnDeleteSupplier');}
		},
		
		modelStatus:{
			add:'0',
			update:'1'
		},

		
		initialize:function(){
			
		},
		
		render:function(){
			$(this.el).html(this.template({item:this.supplierModel.toJSON()}));
			
			this.subControls.btnAddSupplier().on('click',$.proxy(this.addSupplier,this));
			this.subControls.btnUpdateSupplier().on('click',$.proxy(this.updateSupplier,this));
			this.subControls.btnDeleteSupplier().on('click',$.proxy(this.deleteSupplier,this));
		},
		
		/*显示模态框*/
		show:function(id){
			if(id==null){//没有传供应商ID，则断言为添加操作
				//模态框重新绘制
				this.render();
				
				//模态框刷新操作
				this.refresh();
				
				$(this.modelDialog).modal('show');
			}else{
				this.bindData(id);
			}
			
		},
		
		showDialog:function(){
			$(this.modelDialog).modal('show');
		},
		
		/*隐藏模态框*/
		hide:function(){
			$(this.modelDialog).modal('hide');
		},
		
		/*数据绑定*/
		bindData:function(id){
			var _this=this;
			
			//获取模型数据
			this.supplierModel.getData(id,{
				success:function(model,resp){
					//模态框重新绘制
					_this.render();
					
					_this.showDialog();
					//存在供应商ID，则断言保存操作，设置模态框为保存状态
					_this.setStatus(_this.modelStatus.update);
				}
			});
		},
		
		/*刷新模态框*/
		refresh:function(){
			this.supplierModel.clear();
			
			/*this.subControls.Code().val('');
			this.subControls.Name().val('');
			this.subControls.LogoUrl().val('');
			this.subControls.Location().val('');
			this.subControls.Contact().val('');
			this.subControls.Tel().val('');
			this.subControls.Description().val('');*/
			
			this.subControls.Code().val('1');
			this.subControls.Name().val('珠宝1');
			this.subControls.LogoUrl().val('');
			this.subControls.Location().val('深圳罗湖水贝田贝四路1');
			this.subControls.Contact().val('张三1');
			this.subControls.Tel().val('13424406381');
			this.subControls.Description().val('裸石/成品批发1');
			
			this.setStatus(this.modelStatus.add);
		},
		
		/*切换状态开关*/
		setStatus:function(modelStatus){
			switch(modelStatus){
				case this.modelStatus.add:
					this.subControls.Code().removeAttr('disabled');
					this.subControls.btnAddSupplier().removeClass('display-none');
					this.subControls.btnUpdateSupplier().addClass('display-none');
					this.subControls.btnDeleteSupplier().addClass('display-none');
					break;
				case this.modelStatus.update:
					this.subControls.Code().attr('disabled','disabled');
					this.subControls.btnUpdateSupplier().removeClass('display-none');
					this.subControls.btnDeleteSupplier().removeClass('display-none');
					this.subControls.btnAddSupplier().addClass('display-none');
					break;
			}
		},
		
		/*添加供应商*/
		addSupplier:function(){
			
			var _this=this;
			
			var supplierM=this.supplierModel;
			supplierM.set("Code",this.subControls.Code().val());
			supplierM.set("Name",this.subControls.Name().val());
			supplierM.set("LogoUrl",this.subControls.LogoUrl().val());
			supplierM.set("Location",this.subControls.Location().val());
			supplierM.set("Contact",this.subControls.Contact().val());
			supplierM.set("Tel",this.subControls.Tel().val());
			supplierM.set("Description",this.subControls.Description().val());
			
			supplierM.createData({
				success:function(resp){
					alert(resp.message);
					if(resp.code==0){
						_this.hide();
						rootView.getSubView('supplierView').supplierPageableCollection.refresh();
					}
				}
			});
		},
		
		/*更新供应商*/
		updateSupplier:function(){
			var _this=this;
			
			var supplierM=this.supplierModel;
			supplierM.set("Name",this.subControls.Name().val());
			supplierM.set("LogoUrl",this.subControls.LogoUrl().val());
			supplierM.set("Location",this.subControls.Location().val());
			supplierM.set("Contact",this.subControls.Contact().val());
			supplierM.set("Tel",this.subControls.Tel().val());
			supplierM.set("Description",this.subControls.Description().val());
			
			supplierM.updateData({
				success:function(resp){
					alert(resp.message);
					if(resp.code==0){
						_this.hide();
						rootView.getSubView('supplierView').supplierPageableCollection.refresh();
					}
				}
			});
		},
		
		/*删除供应商*/
		deleteSupplier:function(){
			var _this=this;
			
			var supplierM=this.supplierModel;			
			supplierM.deleteData(supplierM.toJSON().Code,{
				success:function(resp){
					alert(resp.message);
					if(resp.code==0){
						_this.hide();
						rootView.getSubView('supplierView').supplierPageableCollection.refresh();
					}
				}
			});
		}
	});
	
	return supplierModelDialogView;
});
